home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / tch_tpas.zip / TL03.TXT < prev    next >
Text File  |  1986-04-05  |  8KB  |  211 lines

  1. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 12
  2.  
  3.                                
  4.                 TURBO-LESSON 3:  PROGRAM STRUCTURE
  5.  
  6. OBJECTIVES - In this lesson you will learn about:
  7.  
  8.    1.  Program structure
  9.    2.  Compiler Directive, $U+
  10.    3.  Write and WriteLn statements
  11.    4.  Comments - at beginning of program
  12.    5.  Comments - at end of statement line
  13.    6.  Comments - kill a section of code temporarily
  14.  
  15.  
  16. 1.  Program structure.
  17.  
  18. PASCAL programs are written in a certain way to make them easier 
  19. to write and easier to read.  There are two parts to any PASCAL 
  20. program: the DECLARATIONS part and the PROGRAM BODY.
  21.  
  22. The PROGRAM BODY is where the processing statements occur, and 
  23. any data item used there must first be defined in the 
  24. DECLARATIONS section.  The form of a PASCAL program is
  25.  
  26. PROGRAM Demo;     (Program name - not required)
  27. LABEL             (Labels, if used, go here)
  28. CONST             (Constants are defined here)
  29. TYPE              (Define your own data types here)
  30. VAR               (ALL variables must be defined here)
  31.  
  32. BEGIN             (Processing statements occur between
  33. END.               BEGIN and END.)
  34.  
  35. The simplest PASCAL program is:
  36.  
  37. BEGIN
  38. END.
  39.  
  40. ##### DO:
  41.  
  42. Type in the program:
  43.  
  44. BEGIN
  45. END.
  46.  
  47. Exit the editor and type R to execute the program.
  48.  
  49. What happened?  (If you hold the R key down to run the program 
  50. over and over, you may see the message, "running").  
  51.  
  52. Notice that no compile errors or run time errors occurred 
  53. (evidenced by the absence of error messages on the screen).
  54. î
  55. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 13
  56.  
  57.  
  58. Definitely not the most useful program, but it illustrates a 
  59. point: the MAIN BODY is always required and as much of the 
  60. DECLARATIONS section as needed to define the data used.  Since 
  61. this simple program uses no data,  the DECLARATIONS section can 
  62. be omitted.  
  63.  
  64. Standard PASCAL requires that declarations occur in the order 
  65. specified and each type of declaration may occur only once.  
  66. TURBO is a little more forgiving in some respects than standard 
  67. PASCAL (declarations don't have to occur in the specified order, 
  68. and the same type of declaration  may occur more than once).  
  69.  
  70.  
  71. 2.  Compiler Directive, $U+
  72.  
  73. Compiler directives are used to control special features provided 
  74. by the compiler.  The directive, $U+, which occurs at the 
  75. beginning of most of the sample programs, ensures that you will 
  76. be able to stop a program without rebooting.  
  77.  
  78. 3.  Write and WriteLn statements.
  79.  
  80. These processing statements are introduced first, because they 
  81. can be used with messages which require no previous data 
  82. declaration.  This allows you to experiment with some programming 
  83. in the MAIN BODY without any concern for the DECLARATIONS part.  
  84.  
  85. ##### DO:
  86.  
  87. Load PROG3 and use the editor to examine the program.  A part of 
  88. PROG3 is printed below:
  89.  
  90. BEGIN
  91.   WriteLn('* * * * * * * * * * * * * * * * * *');  {Note that comments      }
  92.   WriteLn('*                                 *');  {placed at the end of a  }
  93.   WriteLn('*          TURBO-LESSON 3         *');  {statement line should   }
  94.   WriteLn('*                                 *');  {be terminated on the    }
  95.   WriteLn('*  Edited, Compiled, Executed by  *');  {same line.  A multiline }
  96.   WriteLn('*       (put your name here)      *');  {comment, like the one at}
  97.   WriteLn('*                                 *');  {the beginning of this   }
  98.   WriteLn('* * * * * * * * * * * * * * * * * *');  {program would include   }
  99.   {    some of the non-comment statements, making them ineffective.         }
  100.  
  101. In this section of PROG3, the WriteLn statement is used to 
  102. display messages.  
  103.  
  104. ##### DO:
  105.  
  106. RUN the program to see the messages displayed on the screen.  
  107. (Ctrl-K, Ctrl-D to exit editor, then R to run the program.  Refer 
  108. to lessons 1 and 2 and your reference manual for more help using 
  109. TURBO and the editor.) 
  110. î
  111. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 14
  112.  
  113.  
  114. ##### DO:
  115.  
  116. Use the editor to insert your name in the line indicated and 
  117. run the program again.  
  118.  
  119. To illustrate the difference between WriteLn and Write 
  120. statements, another portion of the program is included below:
  121.  
  122.   WriteLn;
  123.   Write  ('Check carefully when ');
  124.   Write  ('you run this program. ');
  125.   Write  ('How many lines ');
  126.   WriteLn('are printed');
  127.   WriteLn('by this last set of Write and WriteLn statements?');
  128.  
  129. The "Ln" in WriteLn may be viewed as a linefeed or "return the 
  130. cursor to the beginning of the next line".  Note that the 
  131. instruction is WriteLn, not LnWrite.  The order is significant.  
  132. Any message, or other data item within the parentheses following 
  133. WriteLn is printed first, the "Write" part, then the cursor is 
  134. moved to the next line, the "Ln" part.  
  135.  
  136. Notice the first WriteLn in the program segment above.  There is 
  137. nothing in parentheses to print, so this statement is a way to 
  138. print a blank line.  
  139.  
  140. The next three Write statements write on the same line.  Then the 
  141. following WriteLn statement writes it's message and causes the 
  142. cursor to return to the beginning of the next line.
  143.  
  144. The last WriteLn displays it's message on the line where the 
  145. cursor is positioned, and returns the cursor to the next line.
  146.  
  147. ##### DO:
  148.  
  149. Add some WriteLn statements after the BEGIN to insert several 
  150. blank lines at the top of the screen to push the boxed message 
  151. down the screen.
  152.  
  153. Run the program.  Did it work right?
  154.  
  155.  
  156. 4.  Comments - at the beginning of program.
  157.  
  158. There are two ways to indicate comments in a PASCAL program.  The 
  159. curly brackets, { }, may be used to enclose comments.  The 
  160. original comment delimiters, from standard PASCAL are (*  *).  
  161.  
  162. At the beginning of a program, or anywhere that multiple line 
  163. comments are needed, it is convenient to mark the whole block of 
  164. comments with the delimiters at the left margin.   This makes it 
  165. easy to insert or delete comments within the block.
  166. î
  167. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 15
  168.  
  169.  
  170. ##### DO:
  171.  
  172. Add some comments at the beginning of the program. Try both types 
  173. of comment delimiters, { }, and (* *).
  174.  
  175.  
  176. 5.  Comments - at the end of a statement line.
  177.  
  178. Look at the comments following the WriteLn statements in PROG3.  
  179. Short comments may be inserted to clarify the action of one or 
  180. more statements.  NOTICE CAREFULLY that each of these comments 
  181. has to be completed on the same line, if the next line is another 
  182. active processing statement.
  183.  
  184. ##### DO:
  185.  
  186. Add a short comment, {Blank Line}, after one of the WriteLn statements which 
  187. prints a blank line.
  188.  
  189.  
  190. 6.  Comments - kill a section of code.
  191.  
  192. Having two sets of comment delimiters makes it possible to use 
  193. one set for most comments, while reserving the second set for 
  194. "killing" sections of statements when debugging a program.  
  195.  
  196. I prefer to use the { } for most comments, and (*  *) for killing 
  197. code.  You should keep the delimiters used for commenting out 
  198. (killing) code in the left margin where they are very visible.
  199.  
  200. ##### DO:
  201.  
  202. Kill the three WriteLn statements following the one which prints 
  203. TURBO-LESSON 3.
  204.  
  205. Run the program to see the results.  
  206.  
  207. DON'T BE AFRAID TO EXPERIMENT WITH THE SAMPLE PROGRAMS.  YOU CAN 
  208. ALWAYS GET ANOTHER COPY, FROM YOUR BACKUP DISK (you did make one, 
  209. didn't you), FROM THE .BAK FILE PROVIDED BY TURBO, OR FROM THE 
  210. ORIGINAL FILE IF YOU HAVEN'T SAVED AN EDITED COPY.
  211. î